package com.wuyan.locationdog.ui.my


import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_update_userinfo
import com.arcsoft.commonbase.ext.ImageOptions
import com.arcsoft.commonbase.ext.loadImage
import com.arcsoft.commonbase.ext.fileToPart
import com.arcsoft.commonbase.ext.loadImageCircle
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.wuyan.locationdog.databinding.MyPhotoActivityBinding
import com.wuyan.locationdog.util.imageselecter.GlideEngine
import com.wuyan.locationdog.util.imageselecter.ImageCropEngine
import com.wuyan.locationdog.viewmodel.MyViewModel
import okhttp3.RequestBody
import java.io.File


class MyPhotoActivity : BaseBindingActivity<MyPhotoActivityBinding>() {
    private val mViewModel: MyViewModel by viewModels()

    override fun initData() {
        setTitleText("头像详情")
        binding?.apply {
            btnAlbum.setOnClickListener {
                PictureSelector.create(mActivity)
                    .openGallery(SelectMimeType.ofImage())
                    .setImageEngine(GlideEngine())
                    .setCropEngine(ImageCropEngine())
                    .setMaxSelectNum(1)
                    .forResult(object : OnResultCallbackListener<LocalMedia> {
                        override fun onResult(result: ArrayList<LocalMedia>) {
                            val file = File(result[0].availablePath)
                            btnPhoto.loadImage(result[0].availablePath, ImageOptions().apply {
                                circleCrop = true
                            })

                            val map = HashMap<String, RequestBody>()
                            mViewModel.fileUpload(map, fileToPart(file))
                        }
                        override fun onCancel() {}
                    })
            }

            btnCamera.setOnClickListener {
                PictureSelector.create(mActivity)
                    .openCamera(SelectMimeType.ofImage())
                    .setCropEngine(ImageCropEngine())
                    .forResult(object : OnResultCallbackListener<LocalMedia> {
                        override fun onResult(result: ArrayList<LocalMedia>) {
                            val file = File(result[0].availablePath)
                            btnPhoto.loadImage(result[0].availablePath, ImageOptions().apply {
                                circleCrop = true
                            })
                            val map = HashMap<String, RequestBody>()
                            mViewModel.fileUpload(map, fileToPart(file))
                        }
                        override fun onCancel() {}
                    })
            }
        }

        mViewModel.getMyInfo(hashMapOf())

    }

    override fun observe() {
        super.observe()

        mViewModel.fileUploadBean.observe(this) {
            val childMap = HashMap<String, Any>()
            childMap["fileName"] = it.content.fileName
            childMap["fileUrl"] = it.content.fileUrl
            childMap["sortNo"] = 0

            val map = HashMap<String, Any>()
            map["avatarDTO"] = childMap
            mViewModel.updateUserInfo(map)
        }

        mViewModel.anyBean.observe(this) {
            Bus.post(bus_update_userinfo,"")
        }
        mViewModel.myInfoBean.observe(this) {
            binding.apply {
                btnPhoto.loadImageCircle(it.content.avatarDTO.fileUrl)
            }

        }
    }

}